﻿var getReport='/indicator/type/get.do';
var actionGetList='/report/query/self.do';     		//查询上报状态(获得上报时间，上报人，上报状态)

//打印报表功能
function printReports(){
	loadSelectIndicatorType();
	$("#print_div").show();
	$("#print_div").jqprint();
	$("#print_div").hide();
}

//获取报表信息方法
function loadSelectIndicatorType(){
	$("#print_content").empty();
	// 选中了哪些指标类型(指标领域)
	var indicatorTypeIds = $('#indicatorTypeSelect').combobox('getValues')+"";
	var formid=$("#formid").combobox('getValue');

	$.ajax({
		url: getReport,
		async: false,//同步方式发送请求，true为异步发送
		type: "GET",
		dataType: "json",
		data: {"idStr":indicatorTypeIds, "formsid":formid},
		success: function (data) {
			for (var i = 0; i < data.length; i++) {
				// 领域标题
				var title = "<div id='addView_title' class='tc mb30'><h3 style='font-size:24px;font-family:宋体;' id='ih3'><span>"+data[i].name+"</span></h3></div>";

				// 领域标题下方的报表说明
				var head =
				"<div id='addView_head' style='padding-bottom:10px;margin-top:-20px;float:right;'>" +
				"    <ul id='iul'>" +
				"    <li style='width:200px;'>表&nbsp;&nbsp;号：<span>"+data[i].code+"</span></li>" +
				"    <li style='width:200px;'>制定机关：<span>"+data[i].making+"</span></li>" +
				"    <li style='width:200px;'>批准文号：<span>"+data[i].number+"</span></li>" +
				"    <li style='width:200px;'>有效期至：<span>"+data[i].expire+"</span></li>" +
				"    </ul>" +
				"</div>";

				// 数据表头
				var table= "<table id='print_table' width='100%;' style='font-size:14px;font-family:宋体;text-align:center; frame=hsides'>";
				var thead =
					"    <thead >" +
					"        <tr style='margin:5px 0px; height:50px' border:1px solid;>" +
					"            <th style='text-align:center; border:1px solid; border-left:none;'>指标名称</th>" +
					"            <th style='text-align:center; border:1px solid;'>汇总代码</th>" +
					"            <th style='text-align:center; border:1px solid;'>计量单位</th>" +
					"            <th style='text-align:center; border:1px solid;'>小数位</th>" +
					"            <th style='text-align:center; border:1px solid;'>上年同期</th>" +
					"            <th style='text-align:center; border:1px solid;'>本年</th>" +
					"            <th style='text-align:center; border:1px solid;'>资料来源</th>" +
					"            <th style='text-align:center; border:1px solid; border-right:none;'>备注</th>" +
					"        </tr>" +
					"        <tr style='margin:5px 0px; height:50px' border:1px solid;>" +
					"            <td style='text-align:center; border:1px solid; border-left:none;'>甲</td>" +
					"            <td style='text-align:center; border:1px solid;'>乙</td>" +
					"            <td style='text-align:center; border:1px solid;'>丙</td>" +
					"            <td style='text-align:center; border:1px solid;'>丁</td>" +
					"            <td style='text-align:center; border:1px solid;'>1</td>" +
					"            <td style='text-align:center; border:1px solid;'>2</td>" +
					"            <td style='text-align:center; border:1px solid;'>戊</td>" +
					"            <td style='text-align:center; border-right:none;'></td>" +
					"        </tr>" +
					"    </thead>";
				var tbody ="<tbody>";
				var tablesBody = getShowData(data[i].id);
				var tbody_table ="</tbody></table>";

				var explain =
					"<div style='width:100%;height:auto;margin-top: 10px'>"+
					"   <span style='display:block;margin-top: 10px;width:50px' className='cl fl' id='ispan'>说明：</span>" +
					"	<ol className='cb' id='iol'>" +
					"		<li>"+data[i].remark+"</li>" +
					"	</ol>" +
					"</div>";

				// 部门信息：charge、author、phone
				// 报表信息：maker
				// 时间信息：reportDate
				var foot =
					"<br/>"+
					"<table style='width:100%;height:auto;margin-top: 10px'> "+
					"	<tr> "+
					"		<td with='80px'><b>单位负责人：</b>"+$("#print_charge").val()+"</td> "+
					"		<td with='80px'><b>统计负责人：</b>"+$("#print_author").val()+"</td> "+
					"		<td with='80px'><b>填表人：</b>"+$("#maker").val()+"</td> "+
					"		<td with='120px'><b>联系电话：</b>"+$("#print_phone").val()+"</td> "+
					"		<td with='100px'><b>报出日期：</b>"+$("#print_date").val()+"</td> "+
					"	</tr> "+
					"</table>";

				// 加上2个换行
				var lineBr = "<br/><br/>"

				// 内容放到页面
				var bodyContent = title+head+table+thead+tbody+tablesBody+tbody_table+explain+foot+lineBr;
				$("#print_content").append(bodyContent);

			}
		}
	});

	var reportPerson=$("#maker").val();     //填表人
	$('#makerPr').text(reportPerson)
	var reportPerson=$("#maker").val();
	// if(reportPerson==null||reportPerson==""){
	// 	$.messager.alert('系统提示', '请先填入填表人姓名!','warning');
	// 	return;
	// }
}

//---------打印显示报表的数据OK
function getShowData(iTypes){

	var yearSelect =  $("#yearSelect").combobox("getValue");
	var yearStr = (yearSelect-1) +","+ yearSelect;
	var formid=$("#formid").combobox('getValue');   //报表（妇儿、社会）

	var showTableStr="";
	$.ajax({
		url: actionGetList,
		async: false,//同步方式发送请求，true为异步发送
		type: "GET",
		dataType: "json",
		data: {"years": yearStr, "iTypes": iTypes, "formsid":formid},
		success: function (data) {
			if(data.code!=200){
				return;
			}
			data = data["data"].results;
			if(data==null || data.length<1){
				return;
			}
			showTableStr=getShowTableStr(data);
		}
	});

	return showTableStr;
}

function getShowTableStr(data){
	var yearSelect =  $("#yearSelect").combobox("getValue");
	var showTablesBodyStr="";
	for (var i = 0; i < data.length; i++) {
		var remark= data[i].remark==undefined||data[i].remark==null? '':data[i].remark;
		var valueLastYear = getValueByRtype(data[i].rtype, data[i][yearSelect-1]);
		var valueSelect = getValueByRtype(data[i].rtype, data[i][yearSelect]);

		var str="<tr>"+
			"<td style='text-align:left;padding-left:15px;border:1px solid;border-left:none;'>"+data[i].iname+"</td>"+
			"<td style='border:1px solid;'>"+data[i].code+"</td>"+
			"<td style='border:1px solid;'>"+getNotEmpty(data[i].unit)+"</td>"+
			"<td style='border:1px solid;'>"+getNotNull(data[i].places)+"</td>"+
			"<td style='width:50px;border:1px solid;'>"+valueLastYear+"</td>"+
			"<td style='width:50px;border:1px solid;'>"+valueSelect+"</td>"+
			"<td style='border:1px solid;'>"+data[i].sname+"</td>"+
			"<td style='width:100px;border:1px solid;border-right:none;'>"+remark+"</td>"+
			"</tr>";
		showTablesBodyStr = showTablesBodyStr + str;
	}
	return showTablesBodyStr;
}

function getValueByRtype(rtype, value){
	var valueSelect = "";
	/** DATA(1, "数值"), EMPTY(2, "空格"), NO_DATA(3, "暂无数据"), TIME(4, "时间"), */
	if(rtype==2){
		valueSelect="空格";
	}else if(rtype==3){
		valueSelect="暂无数据";
	}else if(rtype==4
		&& !isContains(valueSelect, "：")
		&& !isContains(valueSelect, ":")){
		var hour=parseInt(valueSelect / 60);
		var minute=valueSelect % 60;
		valueSelect = hour+":"+minute;
	}else{
		valueSelect=value;
	}
	return valueSelect;
}

function getNotEmpty(value){
	if(value==null||value=="null"||value==""||value=="NULL"||value==" "){
		return "";
	}
	return value;
}
function getNotNull(value){
	if(value==null||value=="null"||value=="NULL"){
		return "";
	}
	return value;
}